﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Fengsoft.AIBlog.Web.Code;
using Fengsoft.AIBlog.Web.Models;
namespace Fengsoft.AIBlog.Web.Controllers
{
    public class GalleryController : Controller
    {
        private void FillGallery(string[] fileNames,ref GalleryManagerModel model)
        {
            foreach (string filePath in fileNames)
            {
                FileInfo file=new FileInfo(filePath);
                string fileName=file.Name;
                string extension=file.Extension;
                string id=fileName.Replace(extension,string.Empty);
                ThumbnailModel thumbnail=new ThumbnailModel();
                thumbnail.ImageViewerUrl=string.Format("~/blobinfo/imageviewer/{0}",id);
                thumbnail.ThumbnailUrl=string.Format("~/blobinfo/thumbnail/{0}?width=320&height=180",id);
                thumbnail.Title=id.Substring(0,id.Length-4).Replace("-"," ");
                model.Gallery.Add(thumbnail);
            }
        }
        private void FillGallery(string[] fileNames,ref GalleryThumbnailListModel model)
        {
            foreach (string filePath in fileNames)
            {
                FileInfo file=new FileInfo(filePath);
                string fileName=file.Name;
                string extension=file.Extension;
                string id=fileName.Replace(extension,string.Empty);
                ThumbnailModel thumbnail=new ThumbnailModel();
                thumbnail.ImageViewerUrl=string.Format("~/blobinfo/imageviewer/{0}",id);
                thumbnail.ThumbnailUrl=string.Format("~/blobinfo/thumbnail/{0}?width=320&height=180",id);
                thumbnail.Title=id.Substring(0,id.Length-4).Replace("-"," ");
                model.Gallery.Add(thumbnail);
            }
        }
        // GET: Gallery
        public ActionResult Index(string sidx,string sord,int page=1,int rows=16)
        {
            GalleryIndexModel model=new GalleryIndexModel();
            ThumbnailListModel thumbnailList=new ThumbnailListModel();
            //var names=CacheHelper.IdolNames;
            PagerModel pager=new PagerModel();
            if (CacheHelper.IdolNames.Any())
            {
                pager.PageSize=rows;
                pager.TotalRecords=CacheHelper.IdolNames.Count();
                pager.CurrentPage=page;
                var pagedNames=CacheHelper.IdolNames.OrderBy(x => x).Select(x => x).Skip(pager.Skip).Take(pager.PageSize).ToArray();
                foreach (string name in pagedNames)
                {
                    BlobModel blob=CacheHelper.Blobs.Where(x => x.Name==name).OrderByDescending(x => x.ModifiedDate).First();
                    ThumbnailModel thumbnail=new ThumbnailModel();
                    thumbnail.ImageViewerUrl=string.Format("~/gallery/thumbnaillist/{0}",blob.SeoName);
                    thumbnail.ThumbnailUrl=string.Format("~/blobinfo/thumbnail/{0}?width=320&height=180",blob.SeoFileName);
                    thumbnail.Title=blob.Name;
                    thumbnailList.Thumbnails.Add(thumbnail);
                }
            }
            model.ThumbnailList=thumbnailList;
            model.Pager=pager;
            return View(model);
        }
        public ActionResult ThumbnailList(string id,string sidx,string sord,int page=1,int rows=16)
        {
            if (string.IsNullOrWhiteSpace(id))
            {
                return View("index");
            }
            else
            {    
                var images=CacheHelper.Blobs.Where(x => x.SeoName.Equals(id,StringComparison.OrdinalIgnoreCase));
                if (images.Any())
                {
                    GalleryThumbnailListModel model=new GalleryThumbnailListModel();
                    PagerModel pager=new PagerModel();
                    pager.PageSize=rows;
                    pager.TotalRecords=images.Count();
                    pager.CurrentPage=page;
                    var fileNames=images.OrderByDescending(x => x.ModifiedDate).Select(x => x.FileName).Skip(pager.Skip).Take(pager.PageSize).ToArray();
                    FillGallery(fileNames,ref model);
                    model.GalleryName=images.First().Name;
                    model.GalleryPager=pager;
                    return View(model);
                }
                else
                {
                    return View("index");
                }
            }
        }
        [Route("admin/gallerymanager")]
        public ActionResult GalleryManager(string sidx,string sord,int page=1,int rows=16)
        {
            if (SessionHelper.IsAdmin)
            {
                GalleryManagerModel model=new GalleryManagerModel();
                PagerModel pager=new PagerModel();
                pager.PageSize=rows;
                pager.TotalRecords=CacheHelper.Blobs.Count();
                pager.CurrentPage=page;
                var fileNames=CacheHelper.Blobs.OrderBy(x => x.ModifiedDate).Select(x => x.FileName).Skip(pager.Skip).Take(pager.PageSize).ToArray();
                FillGallery(fileNames,ref model);
                model.GalleryPager=pager;
                return View(model);
            }
            else
            {
                return new HttpUnauthorizedResult();
            }
        }
    }
}